﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;

namespace ELab.Controls.flatstyle
{
    internal class BorderDrawer
    {
        // Fields 
        private int WM_ERASEBKGND;
        private int WM_NCPAINT;
        private int WM_PAINT;
        public BorderDrawer()
        {
            this.BorderColor = Color.FromArgb(181, 174, 132);
            WM_NCPAINT = 0x85;
            WM_ERASEBKGND = 20;
            WM_PAINT = 15;
        }

        public void DrawBorder(ref System.Windows.Forms.Message message, int width, int height)
        {
            if (((message.Msg == WM_NCPAINT) || (message.Msg == WM_ERASEBKGND)) || (message.Msg == WM_PAINT))
            {
                IntPtr wParam = message.WParam;
                IntPtr hdc = GetDCEx(message.HWnd, wParam, 0x21);
                if (hdc != IntPtr.Zero)
                {
                    Graphics graphics = Graphics.FromHdc(hdc);
                    Rectangle bounds = new Rectangle(0, 0, width, height);
                    ControlPaint.DrawBorder(graphics, bounds, this.BorderColor, ButtonBorderStyle.Solid);
                    message.Result = (IntPtr)1;
                    ReleaseDC(message.HWnd, hdc);
                }
            }
        }



        [DllImport("user32.dll")]
        private static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgnclip, uint fdwOptions);
        [DllImport("user32.dll")]
        private static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);

        // Properties
        public Color BorderColor { get; set; }
    }
}
